package com.etone.smartAudit.controller;

import cn.hutool.core.util.ZipUtil;
import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.dto.projectFile.*;
import com.etone.smartAudit.service.CpuUserService;
import com.etone.smartAudit.service.FileUploadService;
import com.etone.smartAudit.service.ProjectFileService;
import com.etone.smartAudit.vo.FileVO;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.officefile.FileMode;
import com.etone.smartAudit.vo.projectFile.ProjectDetail;
import com.etone.smartAudit.vo.projectFile.ProjectFileDirVO;
import com.etone.smartAudit.vo.projectFile.ProjectFileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * <p>
 * 项目文件表 项目文件表 前端控制器
 * </p>
 *
 * @author jiayen
 * @since 2021-05-08
 */
@RestController
@Api(tags = "项目文件管理", value = "项目文件")
public class ProjectFileController {
    @Autowired
    private ProjectFileService projectFileService;

    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    private CpuUserService cpuUserService;

    @ApiOperation(value = "获取文件树", tags = "jiayen")
    @GetMapping(value = "/projectFile/treeFiles")
    public ProjectFileDirVO fileList(@ApiIgnore @LoginAdmin Admin admin,
                                     @ApiParam("0-非私人文件(项目文件)，1-私人文件(我的文件)") Integer isPrivate
    ) {
        return projectFileService.fileList(admin, isPrivate);
    }

    @ApiOperation(value = "根据父级id获取子文件或者文件夹", tags = "jiayen")
    @GetMapping(value = "/projectFile/fileList/{pId}")
    public PagerVO<ProjectFileVO> getFilesByPId(
            @ApiIgnore @LoginAdmin Admin admin,
            @PathVariable Integer pId,
            @ApiParam(value = "项目id") @RequestParam(required = false, defaultValue = "1") int projectId,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "文件名称，搜索使用") @RequestParam(required = false) String fileName,
            @ApiParam("0-非私人文件(项目文件)，1-私人文件(我的文件) ,-1为获取所有") @RequestParam(required = false, defaultValue = "-1") Integer isPrivate,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10000") int pageSize) {

        return projectFileService.getFilesByPId(admin, pId, page, pageSize, projectId, fileName, isPrivate);
    }

    @PostMapping(value = "/projectFile/createFile")
    @ApiOperation(value = "创建文件夹或者文件", tags = "jiayen")
    public ResponseEntity createFile(@ApiIgnore @LoginAdmin Admin admin
            , @RequestBody ProjectFileDirDTO projectFileDTO) {
        //创建文件夹需要判断是否能创建
        return projectFileService.createFile(admin, projectFileDTO);
    }

    //保存文件
    @PostMapping(value = "/projectFile/saveFile/{pId}")
    @ApiOperation(value = "保存文件", tags = "jiayen")
    public ResponseEntity saveFile(@ApiIgnore @LoginAdmin Admin admin,
                                   @ApiParam("父级id，在当前文件夹得主键id") @PathVariable Integer pId,
                                   @ApiParam(value = "项目id") @RequestParam(required = false, defaultValue = "1") int projectId
            , @RequestBody List<FileVO> fileVOS) {
        //创建文件夹需要判断是否能创建
        return projectFileService.saveFile(admin, fileVOS, pId, projectId);
    }

    @PutMapping(value = "/projectFile/renameFile/{id}")
    @ApiOperation(value = "重命名文件，或者文件夹", tags = "jiayen")
    public void fileRename(@ApiIgnore @LoginAdmin Admin admin,
                           @ApiParam("文件主键id") @PathVariable Integer id
            , @ApiParam("文件名称") @RequestParam("fileName") String fileName) {
        projectFileService.fileRename(admin, id, fileName);
    }

    @ApiOperation(value = "删除文件")
    @DeleteMapping(value = "/projectFile/remove/{id}")
    public void deleteFile(@ApiIgnore @LoginAdmin Admin admin,
                           @ApiParam("文件主键id") @PathVariable Integer id) {
        projectFileService.deleteFile(admin, id);
    }

    @ApiOperation(value = "批量删除文件")
    @DeleteMapping(value = "/projectFile/removeBatch")
    public void batchDetailFile(@ApiIgnore @LoginAdmin Admin admin,
                                @ApiParam("文件主键id,多个已逗号隔开") @RequestParam String ids) {
        projectFileService.batchDetailFile(admin, ids);
    }

    @ApiOperation(value = "获取文件，查看文件(获取文件地址，web端重新打开窗口)", tags = "jiayen")
    @GetMapping("/projectFile/view/{id}")
    public String getFile(@ApiIgnore @LoginAdmin Admin admin, @PathVariable Integer id) {
        //查看文件需要带token校验
        return projectFileService.getFile(admin, id);
    }

    @ApiOperation(value = "获取文件，以文本方式预览", tags = "jiayen")
    @GetMapping("/projectFile/viewText/{id}")
    public String viewText(@ApiIgnore @LoginAdmin Admin admin, @PathVariable Integer id) {
        //查看文件需要带token校验
        return projectFileService.viewText(admin, id);
    }

    @ApiOperation(value = "直接进入编辑文件", tags = "jiayen")
    @GetMapping("/projectFile/editFile/{id}")
    public String editFile(@ApiIgnore @LoginAdmin Admin admin, @PathVariable Integer id) {
        //编辑文件 带校验方式
        return projectFileService.editFile(admin, id);
    }

    @ApiOperation(value = "搜索文件或者文件夹", tags = "jiayen")
    @GetMapping("/projectFile/search")
    public PagerVO<ProjectFileVO> searchFile(@ApiIgnore @LoginAdmin Admin admin, SearchFileDto searchFileDto) {

        return projectFileService.searchFile(admin, searchFileDto);
    }

    @ApiOperation(value = "获取officeUrl", tags = "jiayen")
    @GetMapping("/projectFile/office")
    public String getOfficeUrls() {
        return projectFileService.getOfficeUrl();
    }


    @ApiOperation(value = "获取更改人接口", tags = "jiayen")
    @GetMapping("/projectFile/updateUserList")
    public List<UpdateDTO> getUpdateList() {
        return projectFileService.getUpdateList();
    }

    @ApiOperation(value = "获取文件属性详情", tags = "jiayen")
    @GetMapping("/projectFile/projectFileDetail/{fId}")
    public ProjectDetail projectFileDetail(@ApiIgnore @LoginAdmin Admin admin,
                                           @ApiParam("文件id") @PathVariable Integer fId,
                                           @ApiParam(value = "项目id") @RequestParam(required = false, defaultValue = "1") int projectId
    ) {
        return projectFileService.projectFileDetail(admin, fId, projectId);
    }

    /***
     * 下载单个文件
     */
    @ApiOperation(value = "下载文件", tags = "jiayen")
    @GetMapping("/projectFile/download/{fId}")
    public void downloadFile(@ApiParam("文件id") @PathVariable Integer fId, HttpServletResponse response) {
        projectFileService.downloadFile(fId, response);
    }

    /***
     * 批量下载文件
     */
    @ApiOperation(value = "批量下载文件文件", tags = "jiayen")
    @GetMapping("/projectFile/downloads")
    public void downloads(@ApiIgnore @LoginAdmin Admin admin,
                          FileDownloadDTO fileDownloadDTOS, HttpServletResponse response) {
        projectFileService.downloads(admin, fileDownloadDTOS, response);
    }

    @ApiOperation(value = "保存txt文件", tags = "jiayen")
    @PostMapping("/projectFile/saveTxtFile/{fId}")
    public void saveTxtFile(@ApiIgnore @LoginAdmin Admin admin,
                            @ApiParam("文件id") @PathVariable Integer fId,
                            @ApiParam("文档内容") @RequestBody SaveFileDTO content) {
        projectFileService.saveTxtFile(admin, fId, content);
    }

    @ApiOperation(value = "onlyoffice打开文档", tags = "onlyoffice打开文档")
    @GetMapping("/projectFile/openFIle/{fId}")
    public FileMode getFileMode(@ApiIgnore @LoginAdmin Admin admin,
                                @ApiParam("文件id") @PathVariable Integer fId, HttpServletRequest request) {
        return projectFileService.getFileMode(admin, fId, request);
    }

    @ApiOperation(value = "office文档回调地址", tags = "office文档回调地址")
    @PostMapping("/projectFile/openFIle/forceSave")
    public void changeUrl(HttpServletRequest request, HttpServletResponse response) {
        try {
            projectFileService.changeUrl(request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "使用cpu", tags = "jiayen")
    @GetMapping("/use/cpu")
    public String userCpu(@ApiParam("使用时间，单位秒") @RequestParam Integer time) throws InterruptedException {
        cpuUserService.useCpu(time);
        return "调用成功";
    }
}
